package com.tupu.service.impl.dispatch;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tupu.entity.dispatch.StandardizationWarningSignal;
import com.tupu.mapper.dispatch.StandardizationWarningSignalMapper;
import com.tupu.service.dispatch.StandardizationWarningSignalService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.DecimalFormat;
import java.util.*;
import java.util.stream.Collectors;


@Service
public class StandardizationWarningSignalServiceImpl extends ServiceImpl<StandardizationWarningSignalMapper, StandardizationWarningSignal> implements StandardizationWarningSignalService {

    @Autowired(required = false)
    private StandardizationWarningSignalMapper standardizationWarningSignalMapper;

    @Override
    public Page<StandardizationWarningSignal> warningSignalPage(String signal_type, String device, String problem_type, Long page, Long size) {
        QueryWrapper<StandardizationWarningSignal> queryWrapper = new QueryWrapper<>();
        Page<StandardizationWarningSignal> info = new Page<>(page, size);
        if (StringUtils.isNotEmpty(signal_type)) {
            queryWrapper.eq("signal_type", signal_type);
        }
        if (StringUtils.isNotEmpty(device)) {
            queryWrapper.eq("device", device);
        }
        if (StringUtils.isNotEmpty(problem_type)) {
            queryWrapper.eq("problem_type", problem_type);
        }
        standardizationWarningSignalMapper.selectPage(info, queryWrapper);
        return info;
    }

    @Override
    public List<String> findAllDevice() {
        List<String> list = new ArrayList<>();
        QueryWrapper<StandardizationWarningSignal> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("device");
        List<StandardizationWarningSignal> problemLists = standardizationWarningSignalMapper.selectList(queryWrapper);
        for (StandardizationWarningSignal info : problemLists) {
            if (null != info){
                list.add(info.getDevice());
            }
        }
        List<String> stringList = list.stream().distinct().collect(Collectors.toList());
        stringList.remove("");
        return stringList;
    }

    @Override
    public List<String> findAllProblemType() {
        List<String> list = new ArrayList<>();
        QueryWrapper<StandardizationWarningSignal> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("problem_type");
        List<StandardizationWarningSignal> problemLists = standardizationWarningSignalMapper.selectList(queryWrapper);
        for (StandardizationWarningSignal info : problemLists) {
            if (null != info){
                list.add(info.getProblemType());
            }
        }
        List<String> stringList = list.stream().distinct().collect(Collectors.toList());
        stringList.remove("");
        return stringList;
    }


    @Override
    public List<String> findAllSignalType() {
        List<String> list = new ArrayList<>();
        QueryWrapper<StandardizationWarningSignal> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("signal_type");
        List<StandardizationWarningSignal> problemLists = standardizationWarningSignalMapper.selectList(queryWrapper);
        for (StandardizationWarningSignal info : problemLists) {
            if (null != info){
                list.add(info.getSignalType());
            }
        }
        List<String> stringList = list.stream().distinct().collect(Collectors.toList());
        stringList.remove("");
        return stringList;
    }

    @Override
    public Map<String, Object> devicePercentage() {
        List<String> list = new ArrayList<>();
        QueryWrapper<StandardizationWarningSignal> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("device");
        List<StandardizationWarningSignal> problemLists = standardizationWarningSignalMapper.selectList(queryWrapper);
        for (StandardizationWarningSignal info : problemLists) {
            if (null != info){
                list.add(info.getDevice());
            }
        }
        Map<String, Object> result = new HashMap<>();
        double size = list.size() * 1.0;
        Map<String, Long> map = list.stream().collect(Collectors.groupingBy(k -> k, Collectors.counting()));
        Set<String> strings = map.keySet();
        for (String s : strings) {
            DecimalFormat decimalFormat = new DecimalFormat("##.00%");
            String format = decimalFormat.format(map.get(s) / size);
            result.put(s, format);
        }
        return result;
    }

    @Override
    public Map<String, Object> problemTypePercentage() {
        List<String> list = new ArrayList<>();
        QueryWrapper<StandardizationWarningSignal> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("problem_type");
        List<StandardizationWarningSignal> problemLists = standardizationWarningSignalMapper.selectList(queryWrapper);
        for (StandardizationWarningSignal info : problemLists) {
            if (null != info){
                list.add(info.getProblemType());
            }
        }
        Map<String, Object> result = new HashMap<>();
        double size = list.size() * 1.0;
        Map<String, Long> map = list.stream().collect(Collectors.groupingBy(k -> k, Collectors.counting()));
        Set<String> strings = map.keySet();
        for (String s : strings) {
            DecimalFormat decimalFormat = new DecimalFormat("##.00%");
            String format = decimalFormat.format(map.get(s) / size);
            result.put(s, format);
        }
        return result;
    }

    @Override
    public Map<String, Object> signalTypePercentage() {
        List<String> list = new ArrayList<>();
        QueryWrapper<StandardizationWarningSignal> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("signal_type");
        List<StandardizationWarningSignal> problemLists = standardizationWarningSignalMapper.selectList(queryWrapper);
        for (StandardizationWarningSignal info : problemLists) {
            if (null != info){
                list.add(info.getSignalType());
            }
        }
        Map<String, Object> result = new HashMap<>();
        double size = list.size() * 1.0;
        Map<String, Long> map = list.stream().collect(Collectors.groupingBy(k -> k, Collectors.counting()));
        Set<String> strings = map.keySet();
        for (String s : strings) {
            DecimalFormat decimalFormat = new DecimalFormat("##.00%");
            String format = decimalFormat.format(map.get(s) / size);
            result.put(s, format);
        }
        return result;
    }

}